//
//  BadminShopViewController.m
//  DistributorAB
//
//  Created by 主事丫环 on 2019/5/22.
//  Copyright © 2019 主事丫环. All rights reserved.
//

#import "BadminShopViewController.h"
#import "AinvitivePersonModel.h"
#import "WqGaoDeLocationViewController.h"
#import <CoreLocation/CoreLocation.h>
#import "BaseNavViewController.h"
#import "TabBarViewController.h"
@interface BadminShopViewController ()
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *heightHader;
@property (weak, nonatomic) IBOutlet UIImageView *shopImage;
@property (weak, nonatomic) IBOutlet UITextField *shopName;
@property (weak, nonatomic) IBOutlet UITextField *shopPerson;
@property (weak, nonatomic) IBOutlet UITextField *shopPhone;
@property (weak, nonatomic) IBOutlet UIButton *diqubut;
@property (weak, nonatomic) IBOutlet UITextView *xiangxiAddress;
@property (weak, nonatomic) IBOutlet UIButton *jinrubut;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *jinruacrtionwidth;
@property(nonatomic,strong)AinvitivePersonModel *mymodel;
@property(nonatomic,assign)BOOL isedit;
@property(nonatomic,strong)NSMutableDictionary *addressDic;

@end

@implementation BadminShopViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    
    
    
   
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    if (IPHONE_X) {
        self.heightHader.constant = 200-64+88;
    }
    self.mymodel = [[AinvitivePersonModel alloc]init];
    self.addressDic = [NSMutableDictionary dictionaryWithCapacity:1];
    self.shopName.userInteractionEnabled =NO;
    self.shopPhone.userInteractionEnabled =NO;
    self.shopPerson.userInteractionEnabled =NO;
    self.xiangxiAddress.userInteractionEnabled =NO;
    self.isedit = NO;
    
    if (!_isedit) {
        [self.jinrubut setImage:imgname(@"") forState:(UIControlStateNormal)];
        self.jinruacrtionwidth.constant = 10;
    }else{
        [self.jinrubut setImage:imgname(@"jinrudfdf") forState:(UIControlStateNormal)];
        self.jinruacrtionwidth.constant = 30;
    }
    
    [self requser];
    self.view.backgroundColor = [UIColor colorWithHexString:@"#FCFCFC"];
    
    UITapGestureRecognizer *rtr = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(seleImage)];
    self.shopImage.userInteractionEnabled = YES;
    [self.shopImage addGestureRecognizer:rtr];
}
-(void)seleImage{
    if (_isedit) {
        
    
    //选择logo照片
    UIImagePickerController *pick = [[UIImagePickerController alloc]init];
    pick.allowsEditing = YES;
    pick.delegate = self;
    [UIAlertController showPhotoAlerControllerPicker:pick controller:self sourceView:nil];
    }
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    
    UIImage *image = info[UIImagePickerControllerEditedImage];
    
    
    [self upLoadHeadIcon:image];
    [picker dismissViewControllerAnimated:YES completion:nil];
}
- (void)upLoadHeadIcon:(UIImage *)image {

    self.shopImage.image = image;

    
}

-(void)requser{
    kWeakSelf;
    [NetWorkTool POST:BgetMyShopInfo param:nil success:^(id dic) {
        KMyLog(@"为什么啊 %@",dic);
        [self.mymodel setValuesForKeysWithDictionary:[dic objectForKey:@"data"]];
        
        [weakSelf.shopImage sd_setImageWithURL:[NSURL URLWithString:weakSelf.mymodel.shop_image] placeholderImage:defaultImg];
        
        weakSelf.shopPhone.text = weakSelf.mymodel.shop_phone;
        weakSelf.shopName.text = weakSelf.mymodel.shop_name;
        weakSelf.shopPerson.text = weakSelf.mymodel.person;
        [weakSelf.diqubut setTitle:weakSelf.mymodel.areaAddress forState:(UIControlStateNormal)];
        weakSelf.xiangxiAddress.text = weakSelf.mymodel.shop_address;
        



    } other:^(id dic) {
        ShowToastWithText(dic[@"msg"]);
    } fail:^(NSError *error) {
        
    } needUser:YES];

}
- (IBAction)returnaction:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
//- (IBAction)xiugaiaction:(UIButton *)sender {
//    _isedit = !_isedit;
//    
//    if (!_isedit) {
//        [self.jinrubut setImage:imgname(@"") forState:(UIControlStateNormal)];
//        self.jinruacrtionwidth.constant = 8;
//        [sender setTitle:@"修改" forState:(UIControlStateNormal)];
//        [self updateshopinfomation];
//
//        
//    }else{
//        [self.jinrubut setImage:imgname(@"jinrudfdf") forState:(UIControlStateNormal)];
//        self.jinruacrtionwidth.constant = 30;
//        [sender setTitle:@"保存" forState:(UIControlStateNormal)];
//    }
//    self.shopName.userInteractionEnabled =_isedit;
//    self.shopPhone.userInteractionEnabled =_isedit;
//    self.shopPerson.userInteractionEnabled =_isedit;
//    self.xiangxiAddress.userInteractionEnabled =_isedit;
//    if (!_isedit) {
//        [self.jinrubut setImage:imgname(@"") forState:(UIControlStateNormal)];
//        self.jinruacrtionwidth.constant = 10;
//    }else{
//        [self.jinrubut setImage:imgname(@"jinrudfdf") forState:(UIControlStateNormal)];
//        self.jinruacrtionwidth.constant = 30;
//    }
//}
-(void)updateshopinfomation{
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    //param[@"type"] = @"logo";
    NSMutableDictionary *mudic = [NSMutableDictionary dictionaryWithCapacity:1];
    mudic[@"agent_id"]=  KUID;
    mudic[@"name"]=  _shopName.text;
    mudic[@"telephone"]=  _shopPhone.text;

    mudic[@"address"]=   _xiangxiAddress.text;
    mudic[@"person"]=   _shopPerson.text;
    mudic[@"id"]=   _mymodel.Id;

    if ([self.addressDic allKeys].count >1) {
        mudic[@"lon"]=  [_addressDic objectForKey:@"longitude"];
        mudic[@"lat"]=   [_addressDic objectForKey:@"latitude"];
        mudic[@"area"]=  [_addressDic objectForKey:@"area"];

    }else{
        mudic[@"lon"]= NOTNIL(_mymodel.lon);
        mudic[@"lat"]=  NOTNIL(_mymodel.lat);
        mudic[@"area"]=  NOTNIL(_mymodel.shop_area);
    }
    
    NSString *jsion = [HFTools toJSONString:mudic];
    
    param[@"data"] = jsion;
    kWeakSelf;
    [NetWorkTool UploadPicWithUrl:BupdateMyShop param:param key:@"image" image:_shopImage.image withSuccess:^(id dic) {
        
        [MBProgressHUD hideHUDForView:    weakSelf.view animated:YES];
        ShowToastWithText(@"修改成功");
     
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:weakSelf.view animated:YES];
        
    }];
    
    
    
    
}

- (IBAction)butsdfAction:(UIButton *)sender {
    if (_isedit) {
        [self seleaddressdsfs];

    }
}

- (IBAction)butsele2action:(UIButton *)sender {
    if (_isedit) {
        [self seleaddressdsfs];
        
    }
}



-(void)seleaddressdsfs{
    //选择地址
    if ([self isLocationServiceOpen]) {
        
        
        WqGaoDeLocationViewController *locaVC = [[WqGaoDeLocationViewController alloc]init];
        BaseNavViewController * navigation = [[BaseNavViewController alloc]initWithRootViewController:locaVC];
        //        [self.logoBUt setTitle:[locaVC.locadic objectForKey:@"building_name"] forState:UIControlStateNormal];
        locaVC.locaBlock = ^(NSMutableDictionary *locadic) {
            
            NSString *ssqj = [NSString stringWithFormat:@"%@ %@ %@",[locadic objectForKey:@"province"],[locadic objectForKey:@"city"],[locadic objectForKey:@"district"],[locadic objectForKey:@"building_name"]];
            
            if ([[locadic allKeys] containsObject:@"locadic"]) {
                NSString *afff = [NSString stringWithFormat:@"%@",[locadic objectForKey:@"address"]];
                [self.diqubut setTitle:afff forState:UIControlStateNormal];
                
            }else{
                [self.diqubut setTitle:ssqj forState:UIControlStateNormal];
                
            }
            
            
            //            [self.locadic removeAllObjects];
            self.addressDic = locadic;
            KMyLog(@"看看都有啥%@",locadic);
        };
        [self presentViewController:navigation animated:YES completion:^{
            //        [self.locationButton setTitle:[locaVC.locadic objectForKey:@"building_name"] forState:UIControlStateNormal];
            //        NSLog(@"确定kkkkkkkkkk00%@",locaVC.locadic);
            //
            //        self.locadic = locaVC.locadic;
        }];
    }else{
        //未开启定位权限
        UIAlertController *alertCon = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"需要您开启定位权限,是否允许" preferredStyle:UIAlertControllerStyleAlert];
        
        [alertCon addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            KMyLog(@"取消");
        }]];
        [alertCon addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            KMyLog(@"确定");
            if ([[[UIDevice currentDevice] systemVersion] integerValue] >9.9) {
                
                [[UIApplication sharedApplication]openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
                
            }else
                
            {
                [[UIApplication sharedApplication]openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
                
                //
                //                NSURL *url = [NSURL URLWithString:@"prefs:root=LOCATION_SERVICES"];
                //
                //                [[UIApplication sharedApplication] openURL:url];
                
            }
            
            
            
            
            
            
        }]];
        
        
    }
    
    
}
//是否开启定位
- (BOOL)isLocationServiceOpen {
    if ([ CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied) {
        return NO;
    } else
        return YES;
}
- (IBAction)uoloadInfation:(UIButton *)sender {
  
    if ( sender.selected ) {
//        [sender setTitle:@"提交信息" forState:(UIControlStateNormal)];
        [self updateshopinfomation];
        self.jinruacrtionwidth.constant = 30;

    }else{
//        [sender setTitle:@"修改信息" forState:(UIControlStateNormal)];

    }
    _isedit=    !_isedit;
    sender.selected = !sender.selected;
    
    self.shopName.userInteractionEnabled =_isedit;
    self.shopPhone.userInteractionEnabled =_isedit;
    self.shopPerson.userInteractionEnabled =_isedit;
    self.xiangxiAddress.userInteractionEnabled =_isedit;
    
}


@end
